package com.yanqu.road.server.gameplayer.module.activity;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityUserDigFloor;
import com.yanqu.road.entity.activity.huashigang.HuaShiGangFloorCellData;
import com.yanqu.road.entity.activity.huashigang.HuaShiGangFloorConfig;
import com.yanqu.road.entity.config.goods.OpenGoodsBagResult;
import com.yanqu.road.entity.enums.activity.huashigang.eHuaShiGangMessageType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eChatContentType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eMessageType;
import com.yanqu.road.logic.bussiness.player.activity.UserFloorBusiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.DigMgr;
import com.yanqu.road.server.manger.activity.peakstronghold.PeakStrongholdActivityMgr;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;
import com.yanqu.road.server.manger.config.GoodsBagMgr;
import com.yanqu.road.server.manger.player.WorldMessageMgr;
import com.yanqu.road.server.pb.WorldMessagePb;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.Map;

/**
 * @Author wpj
 * @Date 2020/12/13
 */
public class ActivityDigModule extends GeneralModule {

    Map<Integer, ActivityUserDigFloor> floorMap;

    public ActivityDigModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        floorMap = UserFloorBusiness.getActivityUserDigFloor(player.getUserId());
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return false;
    }

    @Override
    public void afterLogin() {

    }

    /**
     * 花石纲活动数据
     * @param activityId
     * @return
     */
    public ActivityUserDigFloor getFloorData(int activityId) {
        Map<Integer, HuaShiGangFloorConfig> map = DigMgr.getFloorConfigMap(activityId);
        if (map == null) {
            return null;
        }
        if (!floorMap.containsKey(activityId)) {
            ActivityUserDigFloor activityUserDigFloor = new ActivityUserDigFloor();
            activityUserDigFloor.setUserId(player.getUserId());
            activityUserDigFloor.setFloor(1);
            activityUserDigFloor.setActivityId(activityId);

            HuaShiGangFloorConfig config =  map.get(1);
            activityUserDigFloor.setCellDataList(DigMgr.buildCellDataList(config));
            activityUserDigFloor.setGotBigReward(0);
            activityUserDigFloor.setInsertOption();
            floorMap.put(activityId, activityUserDigFloor);
        }

        return floorMap.get(activityId);
    }

    /**
     * 检查宝库数据 看是否进入下一层
     */
    public Property checkFloor(ActivityInfo activityInfo, int boxId) {
        int activityId = activityInfo.getActivityId();
        ActivityUserDigFloor userFloor = floorMap.get(activityId);
        if (userFloor == null) {
            return new Property();
        }

        Map<Integer, HuaShiGangFloorConfig> map = DigMgr.getFloorConfigMap(activityId);
        if (map == null) {
            return new Property();
        }


        HuaShiGangFloorConfig nowConfig = map.get(userFloor.getFloor());
        if (userFloor.getCellDataList().size() != 9) {
            // 数据异常
            userFloor.setCellDataList(DigMgr.buildCellDataList(nowConfig));
            userFloor.setUpdateOption();
            return new Property();
        }

        boolean next = false;
        for (HuaShiGangFloorCellData cellData : userFloor.getCellDataList()) {
            if (cellData.isOpen() && cellData.isSpecial()) {
                // 判断特殊奖励是否被触发
                next = true;
                break;
            }
        }

        if (!next) {
            for (HuaShiGangFloorCellData cellData : userFloor.getCellDataList()) {
                if (!cellData.isOpen()) {
                    // 如果还有格子未被打开则 不进行生成
                    next = false;
                    break;
                }
            }
        }

        boolean reset = false;
        if (next) {
            userFloor.setFloor(userFloor.getFloor() + 1);
            if (map.get(userFloor.getFloor()) == null) {
                // 如果找不到配置表示已达到最高级
                userFloor.setFloor(1);
                reset = true;
            }
            userFloor.setCellDataList(DigMgr.buildCellDataList(map.get(userFloor.getFloor())));
            userFloor.setUpdateOption();
        }

        if (reset) {
            if (boxId > 0) {
                OpenGoodsBagResult result = GoodsBagMgr.getRandomGoods(boxId);
                userFloor.setGotBigReward(1);
                if (result.getReward().getGoods().size() > 0) {
                    player.getModule(CurrencyModule.class).addCurrency(result.getReward(), eLogMoneyType.DesertTreasure, eLogMoneyType.DesertTreasureDig);
                    StringBuilder rewardBuilder = new StringBuilder();
                    rewardBuilder.append(eHuaShiGangMessageType.DIG_FLOOR_ALL_PASS.getValue());
                    rewardBuilder.append(GameConfig.TALK_SEPARATOR);
                    rewardBuilder.append(PropertyHelper.parsePropertyToString(result.getReward()));
                    WorldMessageProto.WorldMessageTempMsg.Builder msg = WorldMessagePb.parseWorldMessageTempMsg(player.getUserInfo(), eMessageType.StrongholdDesert.getValue(), eChatContentType.Common.getContentType(), rewardBuilder.toString(), System.currentTimeMillis(), activityId, null);
                    WorldMessageMgr.sendCrossActivityChat(msg);
                }
                return result.getReward();
            } else {
                userFloor.setGotBigReward(1);
                Property reward;
                if (activityInfo.getType() == eActivityType.PeakStrongHold.getValue()) {
                    reward = PropertyHelper.randomPropertyByListList(PeakStrongholdActivityMgr.getConfig().getDesertPassRewardList());
                } else {
                    reward = PropertyHelper.randomPropertyByListList(StrongholdActivityMgr.getDesertRewardList());
                }
                if (!reward.isNothing()) {
                    player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.DesertTreasure, eLogMoneyType.DesertTreasureDig);
                    StringBuilder rewardBuilder = new StringBuilder();
                    rewardBuilder.append(eHuaShiGangMessageType.DIG_FLOOR_ALL_PASS.getValue());
                    rewardBuilder.append(GameConfig.TALK_SEPARATOR);
                    rewardBuilder.append(PropertyHelper.parsePropertyToString(reward));
                    WorldMessageProto.WorldMessageTempMsg.Builder msg = WorldMessagePb.parseWorldMessageTempMsg(player.getUserInfo(), eMessageType.StrongholdDesert.getValue(), eChatContentType.Common.getContentType(), rewardBuilder.toString(), System.currentTimeMillis(), activityId, null);
                    WorldMessageMgr.sendCrossActivityChat(msg);
                    return reward;
                }
            }
        }

        return new Property();
    }

    @Override
    public boolean saveData() {
        for (ActivityUserDigFloor floor : floorMap.values()) {
            if (floor.isInsertOption()) {
                UserFloorBusiness.addData(floor);
            } else if (floor.isUpdateOption()) {
                UserFloorBusiness.updateData(floor);
            }
        }
        return true;
    }
}
